package com.oig.sys.log.controller;

import com.oig.common.module.SysPageInfo;
import com.oig.common.module.SysRequest;
import com.oig.common.validator.GroupAdd;
import com.oig.sys.log.dto.SysAuditLogDto;
import com.oig.sys.log.service.ITbSysAuditLogService;
import com.oig.sys.log.vo.SysAuditLogVo;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "审计日志管理")
@RestController
@RequestMapping("/sysAuditLog")
@Slf4j
public class TbSysAuditLogController {

    private final ITbSysAuditLogService sysAuditLogService;


    @Autowired
    public TbSysAuditLogController(ITbSysAuditLogService sysAuditLogService){
        this.sysAuditLogService = sysAuditLogService;
    }


    @Hidden
    @Operation(summary = "保存审计日志")
    @PostMapping(value = "/addSysAuditLog")
    public void addSysAuditLog(@RequestBody @Validated(GroupAdd.class) SysAuditLogDto logDto){
        log.info("add sys audit Log : {}", logDto);
        sysAuditLogService.addSysAuditLog(logDto);
    }


    @Operation(summary = "查看审计日志")
    @PostMapping(value = "/info")
    public SysPageInfo<SysAuditLogVo> info(@RequestBody SysRequest<SysAuditLogDto> logDto){
        log.info("get sys audit Log info: {}", logDto);
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            if (logDto.getParamDto()==null){
                logDto.setParamDto(new SysAuditLogDto());
            }
            if (logDto.getParamDto().getTenantId()==null){
                logDto.getParamDto().setTenantId(SecurityUtil.getDefineUserMain().getTenantId());
            }
        }
        return sysAuditLogService.getSysAuditLogInfo(logDto);
    }

    @Operation(summary = "根据id查看审计日志")
    @PostMapping(value = "/info/{id}")
    public SysAuditLogVo info(@PathVariable("id") String id) {
        log.info("get audit sysLog info by id: {}", id);
        return sysAuditLogService.getSysAuditLogInfoById(id);
    }
}
